import Vue from "vue";
import VueRouter from "vue-router";
import { getCurrentTimestamp } from "@/utils/methods"
import AnalyticsService from '@/services/analytics';
Vue.use(VueRouter)


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
}
// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [


];
// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
  path: '/',
  component: () => import('@/views/main.vue'),
  name: 'main',
  redirect: { name: 'home' },
  meta: { title: '整体布局' },
  children: [
    { path: '/home', component: () => import('@/views/home/homepage.vue'), name: 'home', meta: { title: '广东省中山市质量计量监督检测所', mod: 'home' } },// 注意这里的动态路径参数 `:id`
    // { path: '/service:title', component:()=> import('@/views/service/serviceItem.vue'), name: 'service', meta: { title: '服务项目', mod: 'service' } },
    { path: `/service`, component: () => import('@/views/service/serviceItem.vue'), name: 'service', meta: { title: '服务项目', mod: 'service' } },
    { path: '/news', component: () => import('@/views/news/newsInfo.vue'), name: 'news', meta: { title: '新闻资讯', mod: 'news' } },
    { path: '/about', component: () => import('@/views/about/aboutUs.vue'), name: 'about', meta: { title: '关于我们', mod: 'about' } },
    { path: '/nation', component: () => import('@/views/nation/nationCenter.vue'), name: 'nation', meta: { title: '国家中心', mod: 'nation' } },
    { path: '/contact', component: () => import('@/views/contact/contactUs.vue'), name: 'contact', meta: { title: '联系我们', mod: 'contact' } },
    { path: '/consult', component: () => import('@/views/consult/consult.vue'), name: 'consult', meta: { title: '采购咨询', mod: 'consult' } },
    { path: '/column', component: () => import('@/views/column/column.vue'), name: 'column', meta: { title: '党建专栏', mod: 'column' } },
    { path: '/fileDown', component: () => import('@/views/filedownload/fileDown.vue'), name: 'fileDown', meta: { title: '文件下载', mod: 'fileDown' } },
  ],

};
const router = new VueRouter({
  mode: 'hash',
  catchReplacements: true,
  routes: globalRoutes.concat(mainRoutes),// (缩写) 相当于 routes: routes
  ignoreDuplicates: true
})
// 添加全局前置守卫
router.beforeEach((to, from, next) => {  
    AnalyticsService.recordPageView(to.name);
    AnalyticsService.sendDataToBackend(to.name)
     window.scrollTo(0, 0);
  if (to.meta && to.meta.title) {
    document.title = to.meta.title; // 设置页面标题
  }
  next(); // 继续导航
})
export default router;